package com.vframework.system.menu.controller;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.StringUtil;
import com.vframework.base.controller.BaseController;
import com.vframework.common.service.CommonService;
import com.vframework.system.menu.model.MenuBean;
import com.vframework.system.menu.service.MenuService;

/***************************************************************
 * Name:        MenuController.java     
 * Create At:   2010-04-22
 * Module:      菜单管理
 * Description: [菜单管理-菜单管理增删改查功能]
 * Author       shanzhitong
 * ------------------------------------------------------------
 * 修改历史
 * 序号  日期  修改人   修改原因
 * 1
 * 2
***************************************************************/
@Controller
@RequestMapping("MenuController")
public class MenuController extends BaseController {
    
    private static final String BASE_PATH = "com/vframework/system/menu/pages/";
	
	@Autowired
	private MenuService menuService;	
	@Autowired
	private CommonService commonService;
	
	/**
	 * Author:shanzhitong
     * Create At:2012-05-29
     * Description: [系统管理-初始页面，查询菜单信息列表]
     * @return String
     */
	@RequestMapping(value = "begin")
	public String begin(Model model)throws Exception {
		return getMenuList(model);
	}
	
	/**
     * Author:shanzhitong
     * Create At:2010-04-22
     * Description: [菜单管理-查询菜单]
     * @return String
     */
    @RequestMapping(value = "getMenuList")
    private String getMenuList(Model model) {
        try {
            List<MenuBean> list = menuService.getMenuList(super.paramMap);
            model.addAttribute("list", list);
        } catch (Exception e) {
            super.message("查询失败: " + e.getMessage());
            return BASE_PATH + "menuList";
        }
        return BASE_PATH + "menuList";
    }
	
    /**
     * Author:shanzhitong
     * Create At:2010-04-22
     * Description: [菜单管理-点击添加按钮，进入添加页面]
     * @return String
     */
    @RequestMapping(value = "add")
    public String add(Model model) {
        try {
            model.addAttribute("menuBean", new MenuBean());
            model.addAttribute("SILIST", commonService.getOrgList(null));
            model.addAttribute("method", "add");
        } catch (Exception e) {
            super.message("查询机构列表失败: " + e.getMessage());
//          return GlobalConstant.LIST_KEY;
        }
        return BASE_PATH + "menuAdd";
    }
    
    @RequestMapping(value = "edit")
    public String edit(Model model) {
        try {
            model.addAttribute("menuBean", menuService.getMenuById(request.getParameter("menuId")));
            model.addAttribute("SILIST", commonService.getOrgList(null));
            model.addAttribute("method", "edit");
        } catch (Exception e) {
            super.message("查询SI失败: " + e.getMessage());
            return null;
        }
        return BASE_PATH + "menuAdd";
    }
    
	/**
     * Author:shanzhitong
     * Create At:2017-07-29
     * Description: [系统管理-初始页面，ajax查询菜单]
     * @return String
     */
	@RequestMapping(value = "getPmenuList")
	@ResponseBody
	public List<MenuBean> getPmenuList() {
	    List<MenuBean> pmenu = null;
		try {
		    String menuLevel = (String)paramMap.get("param_menuLevel");
		    if (StringUtil.isEmpty(menuLevel)) {
		        return null;
		    }
			pmenu = menuService.getPmenuList(menuLevel);
		} catch (Exception e) {
		    super.message("查询失败: " + e.getMessage());
			return null;
		}
		return pmenu;
	}

	@RequestMapping(value = "addSave")
	public String addSave(MenuBean menuBean, Model model) {
		try {
		    menuBean.setMenuImage(StringEscapeUtils.unescapeHtml4(menuBean.getMenuImage()));
			menuService.addMenu(menuBean);
			super.message("保存成功！");
			menuService.insertLog(super.MakeLogBean(super.LOG_ADD_KEY, "添加菜单", menuBean.toString()));
		} catch (Exception e) {
			super.message("保存失败: " + e.getMessage());
		}
		return getMenuList(model);
	}

	@RequestMapping(value = "editSave")
	public String editSave(MenuBean menuBean, Model model) {
		try {
		    menuBean.setMenuImage(StringEscapeUtils.unescapeHtml4(menuBean.getMenuImage()));
			menuService.editMenu(menuBean);
			super.message("修改成功！");
			menuService.insertLog(super.MakeLogBean(super.LOG_UPDATE_KEY, "修改菜单", menuBean.toString()));
		} catch (Exception e) {
			super.message("修改失败:" + e.getMessage());
		}
		return getMenuList(model);
	}
	
	/**
     * Author:shanzhitong
     * Create At:2017-07-29
     * Description: [系统管理-初始页面，删除菜单]
     * @return String
     */
    @RequestMapping(value = "deleteMenu")
    public String deleteMenu(Model model) {
        try {
            String[] ids = request.getParameterValues("ids");
            super.paramMap.put("ids",ids);
            menuService.deleteMenu(super.paramMap);
            super.message("删除成功！");
            menuService.insertLog(super.MakeLogBean(super.LOG_DELETE_KEY, "删除菜单", super.paramMap.toString()));
        } catch (Exception e) {
            super.message("删除失败: " + e.getMessage());
        }
        return getMenuList(model);
    }
}